Using a match
expression with a boolean condition is considered a bad practice because it adds unnecessary complexity to the code. The
match
expression is designed for pattern matching and is more powerful and flexible than an if-else
construct. However, when
used with a simple boolean condition, it introduces extra verbosity and reduces code readability. Instead, it is generally better to use an
if-else
construct.
Noncompliant code example
fn check_value(value: bool) -> &'static str {
match value {
true => "Value is true",
false => "Value is false",
}
}
Compliant solution
fn check_value(value: bool) -> &'static str {
if value {
"Value is true"
} else {
"Value is false"
}
}